//
//  NWPictureAnimator.swift
//  Weibo01
//
//  Created by 牛威 on 15/12/17.
//  Copyright © 2015年 牛威. All rights reserved.
//

import UIKit


//MARK: - 自定义转场动画的协议

protocol NWPictureAnimatorDelegate : NSObjectProtocol {

    /// 指定对应位置的imageView 用来做转场动画
    
    func imageViewForPresent(indexPath : NSIndexPath) -> UIImageView
    
    /// 动画的转场的起始位置

    func pictureAnimatorDelegateFrom(indexPath : NSIndexPath) ->CGRect
    
    /// 动画的转场的目的位置
    
    func pictureAnimatorDelegateTo(indexPath : NSIndexPath) ->CGRect

}

//MARK: - 自定义转场动画的协议

protocol NWPictureAnimatorDismissDelegate : NSObjectProtocol {


    func imageViewForDismiss()->UIImageView
    
    func indexPathForDismiss()->NSIndexPath

}





//MARK: - 实现转场动画操作

 /// 提供转场动画代理

class NWPictureAnimator: NSObject,UIViewControllerTransitioningDelegate {
    
    //定义代理相关属性
    weak var presentDelegate : NWPictureAnimatorDelegate?
    weak var dismissDelegate : NWPictureAnimatorDismissDelegate?
    //定义图片的索引属性
    var indexPath : NSIndexPath?
    
    //定义一个方法进行初始化和赋值
    
    func setParams(presentDelegate : NWPictureAnimatorDelegate,
        indexPath:NSIndexPath,
        dismissDelegate:NWPictureAnimatorDismissDelegate){ //一个参数一行-->谷歌公司的规定
    
        self.presentDelegate = presentDelegate
        self.indexPath = indexPath
        self.dismissDelegate = dismissDelegate
    
    }
    
    
    //区分到底是present还是dismiss
    
    private var isPresented  = false

    
     //返回 提供modal, "展现自定义转场遵守协议的的对象" 还有一个是提供dismiss的动画方法

    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresented = true
        
        return self
    }
    
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresented = false
        
        return self
    }

}

//MARK: - 自定义动画的实现

extension NWPictureAnimator : UIViewControllerAnimatedTransitioning {



    //动画时长
    
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        
        return 0.5
    }
    
    
    
    ///返回怎么样的动画 一旦实现了此方法,所有动画代码交由程序员负责
    ///
    /// - parameter transitionContext: 转场动画上下文,提供转场动画实现的素材
    
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
        
        isPresented ? presentAnimation(transitionContext) : dismissAnimation(transitionContext)
    }
    
    
    
    //MARK: - present动画的实现
    
    private func presentAnimation(transitionContext: UIViewControllerContextTransitioning){
        
        //需要的素材 选中的图片 开始位置 目标位置
        
        guard let presentDelegate = presentDelegate,indexPath = indexPath else {
        
        
           return
        
        }
        
        
        //设置目标视图
    
        let to = transitionContext.viewForKey(UITransitionContextToViewKey)!
      
             transitionContext.containerView()?.addSubview(to)
        
        //设置选中的动画视图
        
        let image = presentDelegate.imageViewForPresent(indexPath)
        
        //指定起始位置
        
        image.frame = presentDelegate.pictureAnimatorDelegateFrom(indexPath)
        
        //添加到容器
        
        transitionContext.containerView()?.addSubview(image)
        
        
           to.alpha = 0
        
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            
            
            image.frame = presentDelegate.pictureAnimatorDelegateTo(indexPath)

           to.alpha = 1
            
            }) { (_) -> Void in
                
                image.removeFromSuperview()
                
                transitionContext.completeTransition(true)

        }
    
    }

    //MARK: - 解除modal动画的实现
    
    private func dismissAnimation(transitionContext: UIViewControllerContextTransitioning){
        
        // guard let会把变量变为局部变量,后续的闭包中不需要self,也不需要解包
        guard let presentDelegate = presentDelegate,
            dismissDelegate = dismissDelegate
             else {
        
           return
        
        }
        //0 获取解除动画索引indexPath
        
        let indexPath = dismissDelegate.indexPathForDismiss()
        
        // 1首先拿到要dimiss掉的view
        
        let from = transitionContext.viewForKey(UITransitionContextFromViewKey)!
        from.removeFromSuperview()
        
        //2取到要dismiss的图像
        
        let image = dismissDelegate.imageViewForDismiss()
        
        //容器控制器
        transitionContext.containerView()?.addSubview(image)
        
        
        
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            
            
            image.frame = presentDelegate.pictureAnimatorDelegateFrom(indexPath)
            
            }) { (_) -> Void in
                
                //移除视图
                
                image.removeFromSuperview()
                
                transitionContext.completeTransition(true)
                
        }
        
    }

}
